﻿using MediatR;
using NPOI.Util;
using S.Authorization.Common.Dto;
using S.Authorization.Domain.Model.Organization;
using S.Authorization.Infrastructure.Interface.Organization;
using S.Authorization.Infrastructure.Interface.SheepFarms;
using S.Authorization.Read.API.Application.Command.OrganizationCommand;
using S.Authorization.Read.API.Application.Command.SheepFarmsCommand;

namespace S.Authorization.Read.API.Application.CommandHandlers.OrganizationCommandHandler
{
    public class HouseFieldCommandHandler:IRequestHandler<HouseFieldCommand,List<SelectDto>>,IRequestHandler<SheepFarmHouseCommand, List<SelectDto>>
    {
        private readonly IFieldModelRepository field;
        private readonly IColumnManagementRepository column;
        private readonly ISheepFarmsRepository sheep;
        public HouseFieldCommandHandler(IFieldModelRepository _field, IColumnManagementRepository _column, ISheepFarmsRepository _sheep)
        {
            this.field = _field;
            this.column = _column;
            this.sheep = _sheep;
        }
        /// <summary>
        /// 根据栋舍显示栏位
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<List<SelectDto>> Handle(HouseFieldCommand request, CancellationToken cancellationToken)
        {
            List<FieldModel> list = field.GetAllAsync().Result;
            list=list.Where(x => (x.BuildingManagementId == request.id||request.id==0)).ToList();
            List<SelectDto> children = list.Select(x => new SelectDto
            {
                value = x.ColumnManagementId,
                label = x.ColumnName,
            }).ToList();
            return children;
        }
        /// <summary>
        /// 根据羊场显示栋舍
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<List<SelectDto>> Handle(SheepFarmHouseCommand request, CancellationToken cancellationToken)
        {
            List<ColumnManagementModel> list = column.GetAllAsync().Result;
            list = list.Where(x => x.SheepFarmCode == request.id || request.id == 0).ToList();
            List<SelectDto> children = list.Select(x => new SelectDto
            {
                value = x.BuildingManagementId,
                label = x.BuildingName,
            }).ToList();
            return children;
        }

        
    }
}
